//
//  ChooseTimeController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/17.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "ChooseTimeController.h"

@interface ChooseTimeController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *TimePickerView;
@property (assign, nonatomic) NSUInteger year;
@property (assign, nonatomic) NSUInteger month;
@property (assign, nonatomic) NSUInteger days;
@property (assign, nonatomic) NSUInteger day;
@property (strong, nonatomic) NSMutableArray * MonthArr;
@property (strong, nonatomic) NSMutableArray * DayArr;
@property (strong, nonatomic) NSString * TimeStr;
@property (strong, nonatomic) NSString * OldTimeStr;

@end

@implementation ChooseTimeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.TimePickerView.delegate = self;
    self.TimePickerView.dataSource = self;
    
    NSDate *date = [NSDate date];
    // 通过该方法计算特定日期月份的天数
    NSCalendar * calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSRange monthRange =  [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    self.days = monthRange.length;
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    
    unsigned int unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;//这句是说你要获取日期的元素有哪些。获取年就要写NSYearCalendarUnit，获取小时就要写NSHourCalendarUnit，中间用|隔开；
    NSDateComponents *d = [cal components:unitFlags fromDate:date];//把要从date中获取的unitFlags标示的日期元素存放在NSDateComponents类型的d里面；
    //然后就可以从d中获取具体的年月日了；
    self.year = [d year];
    self.month = [d month];
    self.day  =  [d day];
    NSLog(@"%ld",self.year);
    NSLog(@"%ld",self.month);
    NSLog(@"%ld",self.day);
    self.TimeStr = [NSString stringWithFormat:@"%ld-%ld-%ld",self.year,self.month,self.day];
    self.OldTimeStr = [NSString stringWithFormat:@"%ld-%ld-%ld",self.year,self.month,self.day];
    for (int i = 0; i < 12-self.month+1; i++) {
        [self.MonthArr addObject:[NSString stringWithFormat:@"%ld",self.month+i]];
    }
    
    for (int i = 0; i < self.days-self.day+1; i++) {
        [self.DayArr addObject:[NSString stringWithFormat:@"%ld",self.day+i]];
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return 1;
    }else if (component == 1){
        return self.MonthArr.count;
    }else{
        return self.DayArr.count;
    }
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return [NSString stringWithFormat:@"%ld",self.year];
    }else if (component == 1){
        return [NSString stringWithFormat:@"%@",self.MonthArr[row]];
    }else{
        return [NSString stringWithFormat:@"%@",self.DayArr[row]];
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
};

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component == 1) {
        NSString * Str = [NSString stringWithFormat:@"%ld-%ld",self.year,row+self.month];
        self.days = [self numberOfDayInMonthWithDateStr:Str];
        [self.DayArr removeAllObjects];
        if ([self.MonthArr[row] floatValue] > self.month) {
            for (int i = 0; i < self.days+1; i++) {
                [self.DayArr addObject:[NSString stringWithFormat:@"%d",i]];
            }
        }else{
            for (int i = 0; i < self.days- self.day+1; i++) {
                [self.DayArr addObject:[NSString stringWithFormat:@"%ld",self.day+i]];
            }
            
        }
        [self.TimePickerView reloadComponent:2];
    }
    
    NSString * date = [NSString stringWithFormat:@"%ld-%@-%@",self.year,self.MonthArr[[self.TimePickerView selectedRowInComponent:1]],self.DayArr[[self.TimePickerView selectedRowInComponent:2]]];
    NSLog(@"选中的时间%@",date);
    self.TimeStr = date;
}

/**
 根据给定的日期返回该月的天数
 
 @param dateStr 特定的日期格式
 @return 该月天数
 */
- (NSInteger)numberOfDayInMonthWithDateStr:(NSString *)dateStr {
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    // 此处根据需求改对应的日期格式
    [dateFormatter setDateFormat:@"yyyy-MM"];
    NSDate * date = [dateFormatter dateFromString:dateStr];
    NSCalendar * calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 通过该方法计算特定日期月份的天数
    NSRange monthRange =  [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    
    return monthRange.length;
}

- (IBAction)ClickOnBtnToDisMissTheController:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)ClickOnBtnToChooseTheDate:(id)sender {
    if ([self.TimeStr isEqual:self.OldTimeStr]) {
        [MBProgressHUD showAlertViewWithText:@"不能选择当天日期作为开始时间"];
        return;
    }
    if (self.timeBlock) {
        self.timeBlock(self.TimeStr);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSMutableArray *)MonthArr {
    if (!_MonthArr) {
        _MonthArr = [NSMutableArray array];
    }
    return _MonthArr;
}

- (NSMutableArray *)DayArr {
    if (!_DayArr) {
        _DayArr = [NSMutableArray array];
    }
    return _DayArr;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
